package com.woniuxy.house.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.house.entity.User;
import com.woniuxy.house.service.UserService;
import com.woniuxy.house.utils.JwtUtil;
import com.woniuxy.house.vo.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 后台管理登录界面
 */
@RestController
@RequestMapping
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;



    @PostMapping("/tologin")
    public ResponseEntity login(@RequestBody User u) throws JsonProcessingException{
        //这里根据账号密码查找用户
        User user = userService.getUser(u);
        if(user==null){//如果user为空

             return new ResponseEntity("500","失败","账号或密码错误");
        }else{
            //到这里表示存在用户，在这里对user是否为管理员进行判断
            //当用户的状态为y的时候 就是管理员
           if(!user.getStatus().equals("y")) {

               return new ResponseEntity("400","不合法","不是管理员");
           }else{

                String jwt = jwtUtil.createJWT(user.getId(), user.getAccount());//生成jwt
               return new ResponseEntity("200","成功",jwt);
           }
        }

    }
}
